
<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addDeptForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width:80%"
          placeholder="请选择"
          @focus="getEmployeeSimple"
          @blur="checkManager"
        >
          <!-- el-option 每一个选项 -->
          <el-option v-for="item in employeeList" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>

      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getdepartment, addDepartment, updateDepartments, getDeptDetailById } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employeeSimple'
export default {

  props: {
    showDialog: {
      type: Boolean
    },
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkRepeatName = async(rule, value, callback) => {
      const { depts } = await getdepartment()
      const pid = this.node.id
      const isRepeat = depts
        .filter(item => item.pid === pid)
        .some(item => item.name === value)

      isRepeat ? callback(new Error('同一部门下不能重名')) : callback()
    }
    const checkRepeatCode = async(rule, value, callback) => {
      const { depts } = await getdepartment()
      //  const code = this.node.code
      const isRepeat = depts.some(item => item.code === value)
      isRepeat ? callback(new Error('code必须是唯一的')) : callback()
    }
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { max: 50, message: '长度不能超过 50', trigger: 'blur' },
          // 第三规则同部门下不能重名
          { trigger: 'blur', validator: checkRepeatName }
        ],
        code: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { max: 50, message: '长度不能超过 50', trigger: 'blur' },
          { trigger: 'blur', validator: checkRepeatCode }
        ],
        manager: [
          { required: true, message: '数据不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { max: 300, message: '长度不能超过 300', trigger: 'blur' }
        ]

      },
      employeeList: []
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      this.employeeList = res
    },
    checkManager() {
      setTimeout(() => {
        // 手动调用验证
        this.$refs.addDeptForm.validateField('manager')
      }, 100)
    },
    async btnOk() {
      // 手动调用校验
      await this.$refs.addDeptForm.validate()

      // 判断是编辑部门还是新增部门
      if (this.formData.id) {
        // 如果有id那么就是编辑
        await updateDepartments(this.formData)
      } else {
        // 没有id就是新增
        // 发出请求
        await addDepartment({ ...this.formData, pid: this.node.id })
      }

      // 弹出提示
      this.$message.success('操作成功')
      // 发出事件 刷新数据
      this.$emit('addDepartments')
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    btnCancel() {
      // form 表单清理函数只管绑定了的数据
      // 后续回显硬加进去的数据则无法清理
      // 我们可以手动将数据 回复原样
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      // 错误校验清理
      this.$refs.addDeptForm.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    // 展示被编辑的数据
    async getDeptDetailById(id) {
      this.formData = await getDeptDetailById(id)
    }
  }
}
</script>

<style>

</style>
